<?php
namespace Admin\Controller;
use Think\Controller;
use Org\Util\Page;
class TeamController extends ExtendController {
    public function index(){
    $imagesModel = D('Team');
        $count      = $imagesModel->count();// 查询满足要求的总记录数
    $Page       = new Page($count,10);// 实例化分页类 传入总记录数和每页显示的记录数(25)
    $Page -> setConfig('header', '条记录');
    $Page -> setConfig('theme', '<li><a>%totalRow% %header%</a></li> <li>%upPage%</li> <li>%downPage%</li> <li>%first%</li>  <li>%prePage%</li>  <li>%linkPage%</li>  <li>%nextPage%</li> <li>%end%</li> ');//(对thinkphp自带分页的格式进行自定义)
    $show       = $Page->show();// 分页显示输出
     $list = $imagesModel->order('sort asc')->limit($Page->firstRow.','.$Page->listRows)->select();
     if($list){
     	$this->assign('list',$list);
        $this->assign('page',$show);// 赋值分页输出
     }
      $this->display();  
   }

   public function insert(){
   	if(IS_POST){
    extract($_POST);
      $upload = new \Think\Upload();// 实例化上传类
      $upload->maxSize = 10485760;
      $upload->rootPath = './Public/images/';
      $upload->savePath = '';
      $upload->saveName = array('uniqid','');
      $upload->exts     = array('jpg', 'gif', 'png', 'jpeg');
      $upload->autoSub  = true;
      $upload->subName  = array('date','Ymd');
       // 上传文件 
      $info   =   $upload->upload($_FILES);
      if(!$info) {// 上传错误提示错误信息
         $this->error($upload->getError());
      }else{// 上传成功 获取上传文件信息
         foreach($info as $file){
             $info= $file['savepath'].$file['savename'];
             $data['img_url'] = $info;
       }
       $data['name'] = $name;
       $data['introduce'] = $introduce;
       $data['sort'] = $sort;
       $data['createtime'] = date('Y-m-d H:i:s');
        $res = D('Team')->add($data);
        if($res){
        	$this->redirect('Team/index');
        }else{
        	$this->error('操作失败');
        }
      }
   	}else{
   			$this->display();
   	}
   }



      //修改
     public function update() {
        if(IS_POST){
                $where['id'] = $_POST['id'];
                 $data['name'] = $name;
                 $data['introduce'] = $introduce;
                 $data['sort'] = $sort;
                 $data['createtime'] = date('Y-m-d H:i:s');
                    $result = D('Team')->where($where)->save($data);
                   
                    if ($result != false) {
                        $this->redirect('index');
                    } else {
                        $this->error('非法操作!');
                    }
        }else{
            //获取到该条记录的内容
             $list= D('Team')->where('id = '.$_GET['id'])->find();
             if(!$list){
                $this->error('非法操作!');
             }
             $this->assign('list',$list);
                $this->display('update');
                }
}


    //删除
    public function delete() {
        $imagesModel = M('Team');
        $map['id'] = $_GET['id'];
        $result = $imagesModel->where($map)->delete();
        if ($result != false) {
            $this->redirect('Team/index');
        } else {
            $this->error('删除失败!');
        }
    }
}